<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="改单开始时间" prop="startDate" label-width="100">
        <el-date-picker
          clearable
          v-model="queryParams.startDate"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择开始时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="改单结束时间" prop="endDate" label-width="100">
        <el-date-picker
          clearable
          v-model="queryParams.endDate"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择结束时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="老师名字" prop="teacherName">
        <el-input
          v-model="queryParams.teacherName"
          placeholder="请输入老师名字"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="会员卡号" prop="memberCardNumber">
        <el-input
          v-model="queryParams.memberCardNumber"
          placeholder="请输入会员卡号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="军区" prop="militaryArea">
        <el-input
          v-model="queryParams.militaryArea"
          placeholder="请输入军区"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="门店" prop="shopName">
        <el-input
          v-model="queryParams.shopName"
          placeholder="请输入门店"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="改单类型" prop="changeType">
        <el-select @change="handleQuery" clearable v-model="queryParams.changeType" placeholder="请选择">
          <el-option
            v-for="dict in boka_change_type"
            :key="dict.id"
            :label="dict.labelName"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="改单状态" prop="status">
        <el-select @change="handleQuery" clearable v-model="queryParams.status" placeholder="请选择">
          <el-option
            v-for="dict in boka_change_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['business:bokaChangeOrder:export']"
        >导出</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="Plus"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['business:bokaChangeOrder:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="Edit"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['business:bokaChangeOrder:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="Delete"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['business:bokaChangeOrder:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">

      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="bokaChangeOrderList" @selection-change="handleSelectionChange" :row-class-name="tableRowClassName">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="" align="center" prop="id" v-if="true"/>-->
<!--      <el-table-column label="订单id" align="center" prop="orderId" />-->
      <el-table-column label="申请时间" align="center" prop="createTime" />
      <el-table-column label="军区" align="center" prop="militaryArea" />
      <el-table-column label="门店" align="center" prop="shopName" />
      <el-table-column label="老师名字" align="center" prop="teacherName" />
      <el-table-column label="提交人" align="center" prop="createBy" />
      <el-table-column label="客户名字" align="center" prop="memberName" />
      <el-table-column label="会员卡号" align="center" prop="memberCardNumber" />
      <el-table-column label="异动单号" align="center" prop="billNo" />
      <el-table-column label="商品名称" align="center" prop="productName" width="200"/>
      <el-table-column label="商品价格" align="center" prop="productPrice" />
      <el-table-column label="开单时间" align="center" prop="billDate"/>
      <el-table-column label="改单原因" align="center" prop="changeReason" width="200" />
      <el-table-column label="单据照片" align="center" prop="billImg" width="150">
        <template #default="scope">
          <ImagePreview
            :width="100" :height="100"
            :src="scope.row.billImg"
            :preview-src-list="[scope.row.billImg]"/>
        </template>
      </el-table-column>
      <el-table-column label="改单类型" align="center" prop="changeType" />
      <el-table-column label="新商品名称" align="center" prop="newProductName" width="100">
        <template #default="scope">
          <span>{{scope.row.newProductName != '' ? scope.row.newProductName : '/'}}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否统计次数" align="center" prop="isStatisticsCount" width="120"/>
      <el-table-column label="改单时间" align="center" prop="changeDate" />
      <el-table-column label="改单状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="boka_change_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column
        label="已看状态"
        align="center"
        key="readStatus"
      >
        <template #default="scope">
          <el-switch
            v-model="scope.row.readStatus"
            active-value="1"
            inactive-value="0"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right">
          <template #default="scope">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['business:bokaChangeOrder:edit']">编辑</el-button>
<!--              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['business:bokaChangeOrder:remove']">删除</el-button>-->
          </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改博卡申请改单记录对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="bokaChangeOrderRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="改单状态">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in boka_change_status"
              :key="dict.value"
              :label="dict.value"
            >{{ dict.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <div v-if="form.status != 2">
          <el-form-item label="改单类型" prop="changeType">
            <el-select style="width: 240px" clearable v-model="form.changeType" placeholder="请选择">
              <el-option
                v-for="dict in boka_change_type"
                :key="dict.id"
                :label="dict.labelName"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <div v-if="form.changeType == '改老师'">
            <el-form-item label="原老师" prop="teacherName">
              <el-input style="width: 240px" v-model="form.teacherName" disabled />
            </el-form-item>
            <el-form-item label="新老师" prop="newTeacherName">
              <el-select
                v-model="form.newTeacherName"
                filterable
                placeholder="请选择"
                clearable
                :loading="loading2"
                style="width: 240px"
              >
                <el-option
                  v-for="(item,index) in teacherNameList"
                  :key="index"
                  :label="item.easySalesTeacherName"
                  :value="item.easySalesTeacherName"
                />
              </el-select>
            </el-form-item>
          </div>
          <div v-if="form.changeType == '改头发款式' || form.changeType == '改头发尺寸' || form.changeType == '改金额'">
            <el-form-item label="原头发款式" prop="productName">
              <el-input style="width: 240px" v-model="form.productName" disabled placeholder="请输入" />
            </el-form-item>
            <el-form-item label="新头发款式" prop="newProductName">
              <el-select
                v-model="form.newProductName"
                filterable
                placeholder="请选择"
                clearable
                style="width: 240px"
              >
                <el-option
                  v-for="(item,index) in hairList"
                  :key="index"
                  :label="item.goodsName"
                  :value="item.goodsName"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="改单后全款金额" prop="fullPayment">
              <el-input
                style="width: 240px"
                v-model="form.fullPayment"
                type="number"
                placeholder="请输入"
              />
            </el-form-item>
          </div>

          <el-form-item label="是否统计次数" prop="isStatisticsCount">
            <el-select style="width: 240px" clearable v-model="form.isStatisticsCount" placeholder="请选择">
              <el-option label="不计入" value="不计入"></el-option>
              <el-option label="计入" value="计入"></el-option>
            </el-select>
          </el-form-item>
        </div>

      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="BokaChangeOrder">

import {
  listBokaChangeOrder,
  getBokaChangeOrder,
  delBokaChangeOrder,
  addBokaChangeOrder,
  updateBokaChangeOrder,
  changeUserStatus,
  getGoodNameApi
} from "@/api/business/bokaChangeOrder";
import axios from "axios";
const { proxy } = getCurrentInstance();
const {
  boka_change_status,
  boka_change_type
} = proxy.useDict(
  "boka_change_status",
  "boka_change_type"
);
const bokaChangeOrderList = ref([]);
const open = ref(false);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const hairList = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    orderId: undefined,
    militaryArea: undefined,
    shopName: undefined,
    teacherName: undefined,
    memberName: undefined,
    memberCardNumber: undefined,
    productName: undefined,
    productPrice: undefined,
    billDate: undefined,
    changeReason: undefined,
    billImg: undefined,
    status: undefined,
    changeType: undefined,
    newProductName: undefined,
    isStatisticsCount: undefined,
    changeDate: undefined,

  },
  rules: {
    id: [
      { required: true, message: "不能为空", trigger: "blur" }
    ],
    orderId: [
      { required: true, message: "订单id不能为空", trigger: "blur" }
    ],
    militaryArea: [
      { required: true, message: "军区不能为空", trigger: "blur" }
    ],
    shopName: [
      { required: true, message: "门店不能为空", trigger: "blur" }
    ],
    teacherName: [
      { required: true, message: "老师名字不能为空", trigger: "blur" }
    ],
    memberName: [
      { required: true, message: "客户名字不能为空", trigger: "blur" }
    ],
    memberCardNumber: [
      { required: true, message: "会员卡号不能为空", trigger: "blur" }
    ],
    productName: [
      { required: true, message: "商品名称不能为空", trigger: "blur" }
    ],
    productPrice: [
      { required: true, message: "商品价格不能为空", trigger: "blur" }
    ],
    billDate: [
      { required: true, message: "开单时间不能为空", trigger: "blur" }
    ],
    changeReason: [
      { required: true, message: "改单原因不能为空", trigger: "blur" }
    ],
    billImg: [
      { required: true, message: "单据照片不能为空", trigger: "blur" }
    ],
    status: [
      { required: true, message: "处理状态 0 = 待处理，1 = 已处理，2 = 不处理不能为空", trigger: "change" }
    ],
    changeType: [
      { required: true, message: "改单类型不能为空", trigger: "change" }
    ],
    newProductName: [
      { required: true, message: "新商品名称不能为空", trigger: "blur" }
    ],
    isStatisticsCount: [
      { required: true, message: "是否统计次数不能为空", trigger: "blur" }
    ],
    changeDate: [
      { required: true, message: "改单时间不能为空", trigger: "blur" }
    ],
    newTeacherName: [
      { required: true, message: "新老师不能为空", trigger: "blur" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);
const getGoodName = () => {
  getGoodNameApi().then(res => {
    hairList.value = res.data;
  })
}
getGoodName();
/** 查询博卡申请改单记录列表 */
function getList() {
  loading.value = true;
  listBokaChangeOrder(queryParams.value).then(response => {
    bokaChangeOrderList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
function tableRowClassName({row, rowIndex}) {
  if (row.readStatus == '1') {
    return 'success-row';
  }
  return '';
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

/** 阅读状态修改  */
function handleStatusChange(row) {
  // console.log(proxy.$modal)
  // proxy.$modal.then(function () {
  //     return changeUserStatus(row.id,row.readStatus);
  //   })
  //   .then(() => {
  //     proxy.$modal.msgSuccess(text + "成功");
  //   })
  //   .catch(function () {
  //     row.readStatus = row.readStatus === "0" ? "1" : "0";
  //   });
  changeUserStatus(row.id,row.readStatus).catch(()=>{
    row.readStatus = row.readStatus === "0" ? "1" : "0";
  });
}


const teacherNameList = ref([]);
const loading2 = ref(true);
function getTeacherNameList(row) {
  axios.get('https://mp.jikebufa.cn/dev/xcx/wxCp/queryTeacherNameList',{}).then((response) => {
    loading2.value = false;
    teacherNameList.value = response.data.data;
  }).catch((err) => { 
    loading2.value = false;
    proxy.$modal.msgError(err.mag);
  });
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    orderId: null,
    militaryArea: null,
    shopName: null,
    teacherName: null,
    memberName: null,
    memberCardNumber: null,
    productName: null,
    productPrice: null,
    billDate: null,
    changeReason: null,
    billImg: null,
    status: null,
    changeType: null,
    newProductName: null,
    newTeacherName:null,
    isStatisticsCount: null,
    changeDate: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    delFlag: null,
    fullPayment: null
  };
  proxy.resetForm("bokaChangeOrderRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加博卡申请改单记录";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  loading.value = true
  reset();
  const _id = row.id || ids.value
  getBokaChangeOrder(_id).then(response => {
    loading.value = false;
    form.value = response.data;
    open.value = true;
    title.value = "编辑";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["bokaChangeOrderRef"].validate(valid => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id != null) {
        updateBokaChangeOrder(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        }).finally(() => {
          buttonLoading.value = false;
        });
      } else {
        addBokaChangeOrder(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        }).finally(() => {
          buttonLoading.value = false;
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除博卡申请改单记录编号为"' + _ids + '"的数据项？').then(function() {
    loading.value = true;
    return delBokaChangeOrder(_ids);
  }).then(() => {
    loading.value = true;
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  }).finally(() => {
    loading.value = false;
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('business/bokaChangeOrder/export', {
    ...queryParams.value
  }, `bokaChangeOrder_${new Date().getTime()}.xlsx`)
}
getTeacherNameList();
getList();
</script>
<style>
.el-table tbody tr:hover > td {
  background-color: transparent !important
}
.el-table .warning-row {
  background: #d91414;
}

.el-table .success-row {
  background: #59d511;
}
</style>
